package com.loe.logger.util;

import android.util.Log;

public class LoeLog
{
    public static boolean showThread = false;

    public static final String TAG = "LoeLogger";

    public static void d(String tag, String message)
    {
        log(Log.DEBUG, tag, message);
    }

    public static void d(String message)
    {
        log(Log.DEBUG, null, message);
    }

    public static void e(String message)
    {
        log(Log.ERROR, null, message);
    }

    public static void e(String tag, String message)
    {
        log(Log.ERROR, tag, message);
    }

    public static void i(String message)
    {
        log(Log.INFO, null, message);
    }

    public static void i(String tag, String message)
    {
        log(Log.INFO, tag, message);
    }

    public static void w(String message)
    {
        log(Log.WARN, null, message);
    }

    public static void w(String tag, String message)
    {
        log(Log.WARN, tag, message);
    }

    public static void v(String message)
    {
        log(Log.VERBOSE, null, message);
    }

    public static void v(String tag, String message)
    {
        log(Log.VERBOSE, tag, message);
    }

    public static void wtf(String message)
    {
        log(Log.ASSERT, null, message);
    }

    public static void wtf(String tag, String message)
    {
        log(Log.ASSERT, tag, message);
    }

    public static void log(int priority, String tag, String message)
    {
        // 1. 获取堆栈跟踪，跳过 LoeLogger 自身的调用
        StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        StackTraceElement caller = findCaller(stackTrace);

        // 2. 提取类名、方法名、文件名和行号
        String className = caller.getClassName().substring(caller.getClassName().lastIndexOf(".") + 1); // 去掉包名
        String methodName = caller.getMethodName();
        String fileName = caller.getFileName();
        int lineNumber = caller.getLineNumber();

        // 3. 格式化调用路径（如 "MainActivity$onCreate$2.onClick"）
        //        String callPath = formatCallPath(className, methodName);

        // 4. 格式化日志（模仿 Logger 样式）
        printLog(priority, tag, fileName, lineNumber, message);
    }

    private static StackTraceElement findCaller(StackTraceElement[] stackTrace)
    {
        // 跳过 LoeLogger 和 Log 的调用
        for (StackTraceElement element : stackTrace)
        {
            if (!element.getClassName().contains("LoeLog"))
            {
                return element;
            }
        }
        return stackTrace[0]; // 如果找不到，用第一个堆栈帧（保底）
    }

    public static String repeatSpace(int length)
    {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append('═');
        }
        return sb.toString();
    }

    private static void printLog(int priority, String tag, String fileName, int lineNumber, String message)
    {
        tag = tag == null ? TAG : TAG + "-" + tag;
        String threadName = showThread ? "[" + Thread.currentThread().getName() + "]" : "";
        String stackName = "(" + fileName + ":" + lineNumber + ")" + threadName + "";
        String line = repeatSpace(stackName.length());
        Log.println(priority, tag, "");
        Log.println(priority, tag, "╔══════════════════════════════════════════════════ " + stackName + " ══════════════════════════════════════════════════╗");
        String[] messages = message.split("\n");
        for (String s : messages)
        {
            Log.println(priority, tag, "║ " + s);
        }
        Log.println(priority, tag, "╚══════════════════════════════════════════════════════════════════════════════════════════════════════" + line + "╝");
    }
}